﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QldtSdh.BLL
{
    using System.Drawing;
    using DAL.Models;

    class BizAppConfig
    {
        private static string _GetValueOfKey(string key, string defaultValue, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (var db2 = CEMS4UG_DHKHContext.GetContext())
                    return _GetValueOfKey(key, defaultValue, db2);
            else
            {
                var oKeyValueItem = db.KeyValueDbSet.Where(e => e.Key == key).FirstOrDefault();
                if (oKeyValueItem == null)
                {
                    oKeyValueItem = new KeyValueConfig() { Key = key, Value = defaultValue };
                    db.KeyValueDbSet.Add(oKeyValueItem);
                    db.SaveChanges();
                    return oKeyValueItem.Value;
                }
                else
                    return oKeyValueItem.Value;
            }
        }

        private static void _SetValueOfKey(string key, string value, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (var db2 = CEMS4UG_DHKHContext.GetContext())
                    _SetValueOfKey(key, value, db2);
            else
            {
                var oKeyValueItem = db.KeyValueDbSet.Where(e => e.Key == key).FirstOrDefault();
                if (oKeyValueItem == null)
                {
                    oKeyValueItem = new KeyValueConfig() { Key = key, Value = value };
                    db.KeyValueDbSet.Add(oKeyValueItem);
                    db.SaveChanges();
                }
                else
                {
                    oKeyValueItem.Value = value;
                    db.SaveChanges();
                }
            }
        }

        #region Danh sách cảnh báo desktop
        private const string KEY_DS_CANHBAODESKTOP = "DS_CANHBAO_DESKTOP";
        internal static string GetDSCanhBaoDesktop(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_DS_CANHBAODESKTOP, "0", db);
        }

        internal static void SetDSCanhBaoDesktop(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_DS_CANHBAODESKTOP, value, db);
        }
        #endregion

        #region Danh sách cảnh báo mobile
        private const string KEY_DS_CANHBAOMOBILE = "DS_CANHBAO_MOBILE";
        internal static string GetDSCanhBaoMobile(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_DS_CANHBAOMOBILE, "0", db);
        }

        internal static void SetDSCanhBaoMobile(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_DS_CANHBAOMOBILE, value, db);
        }
        #endregion

        #region Màu quá hạn
        private const string KEY_MAU_QUAHAN = "MAU_QUAHAN";
        internal static string GetMAUQUAHAN(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_MAU_QUAHAN, Color.Black.Name, db);
        }

        internal static void SetMAUQUAHAN(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_MAU_QUAHAN, value, db);
        }
        #endregion

        #region Màu quá hạn dưới 30%
        private const string KEY_MAU_QUAHANDUOI30 = "MAU_TOIHANDUOI30";
        internal static string GetMAUQUAHANDUOI30(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_MAU_QUAHANDUOI30, Color.Red.ToArgb().ToString(), db);
        }

        internal static void SetMAUQUAHANDUOI30(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_MAU_QUAHANDUOI30, value, db);
        }
        #endregion

        #region Màu tới hạn
        private const string KEY_MAU_TOIHAN = "MAU_TOIHAN";
        internal static string GetMAUTOIHAN(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_MAU_TOIHAN, Color.Red.Name, db);
        }

        internal static void SetMAUTOIHAN(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_MAU_TOIHAN, value, db);
        }
        #endregion

        #region Màu trước hạn
        private const string KEY_MAU_TRUOCHAN = "MAU_TRUOCHAN";
        internal static string GetMAUTRUOCHAN(CEMS4UG_DHKHContext db = null)
        {
            return _GetValueOfKey(KEY_MAU_TRUOCHAN, Color.Green.Name, db);
        }

        internal static void SetMAUTRUOCHAN(string value, CEMS4UG_DHKHContext db = null)
        {
            _SetValueOfKey(KEY_MAU_TRUOCHAN, value, db);
        }
        #endregion
        internal static void SaveAppConfig(string dsCanhBaoDesktop, string dsCanhBaoMobile, string mauQuaHan, string mauToiHan, string mauTruocHan)
        {
            using (var db = DAL.Models.CEMS4UG_DHKHContext.GetContext())
            {
                SetDSCanhBaoDesktop(dsCanhBaoDesktop, db);
                SetDSCanhBaoMobile(dsCanhBaoMobile, db);
                SetMAUQUAHAN(mauQuaHan, db);
                SetMAUTOIHAN(mauToiHan, db);
                SetMAUTRUOCHAN(mauTruocHan, db);
            }
        }
    }
}
